package models

import (
	"fmt"
	"novel-page/appconfig"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB
var err error

func Init() {
	mysqlC := appconfig.Conf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlC.User,
		mysqlC.Password,
		mysqlC.Host,
		mysqlC.Port,
		mysqlC.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(&User{}, &Content{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库迁移成功")
}
